package com.bite.demo.service;

import com.bite.demo.dao.BookDao;
import com.bite.demo.enums.StatusEnum;
import com.bite.demo.mapper.BookMapper;
import com.bite.demo.model.BookInfo;
import com.bite.demo.model.PageResult;
import com.bite.demo.model.RequestPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class BookService {
    @Autowired
    private BookDao bookDao;

    @Autowired
    private BookMapper bookMapper;
    public List<BookInfo> getList() {
        List<BookInfo> bookInfoList = bookDao.mockData();
        for (BookInfo bookInfo:bookInfoList) {
            if(bookInfo.getStatus()==1) {
                bookInfo.setStatusCN("可借阅");
            } else {
                bookInfo.setStatusCN("不可解约");
            }
        }
        return bookInfoList;
    }

    public Integer addBook(BookInfo bookInfo) {
        Integer ret =bookMapper.insertBook(bookInfo);
        return ret;
    }

    public PageResult<BookInfo> getListByPage(RequestPage requestPage) {
        Integer count = bookMapper.count();
        //int offset = (requestPage.getCurrentPage()-1)*requestPage.getPageSize();
        List<BookInfo> bookInfos = bookMapper.getListByPage(requestPage.getOffset(), requestPage.getPageSize());
        for (BookInfo bookInfo:bookInfos) {
            bookInfo.setStatusCN(StatusEnum.getNameByCode(bookInfo.getStatus()).getName());
        }

        return new PageResult<>(bookInfos,count,requestPage);
    }

    public BookInfo getBookInfo(Integer bookId) {
      return bookMapper.getBookInfo(bookId);
    }

    public Integer updateBook(BookInfo bookInfo) {
        return bookMapper.updateBook(bookInfo);
    }

    public Integer batchDelete(List<Integer> ids) {
        return bookMapper.batchDelete(ids);
    }
}
